<template>
	<view class="goods-details">

		<loading-view v-if="isFirstLoading"></loading-view>

		<view class="contain" v-if="!isNull" id="goods">
			<!-- 	<bubble-tips :top="90 + navHeight + 'rpx'"></bubble-tips> -->
			<product-swiper v-if="swiperList.length" :imgUrls="swiperList"></product-swiper>

			<view class="priceView flex-row-space-between">
				<view class="" style="margin-top: 10rpx;">
					<text class="price" style="margin-right: 4rpx;">¥</text>
					<!-- 整数 -->
					<text class="price1">{{goodsDetail.price}}</text>

					<!-- 单位 -->
					<text class="price">/天起</text>
				</view>
				<view class="flex-column-center" style="width: 200rpx;text-align: center;">
					<!-- <view class="priceTitle">
						首期
					</view>
					<view class="priceTitle1">
						￥450.00
					</view> -->
				</view>
			</view>
			<view class="infoView">
				<view class="name">
					{{ goodsDetail.name }}
				</view>
				<view class="flex-row-flex-start" style="margin: 20rpx 0;">
					<view class="flex-row-flex-start">
						<!-- 		<image class="myImage" src="/static/lease/my.png" mode="scaleToFill"></image> -->
						<view class="price2">
							<!-- 商品押金 ￥{{goodsDetail.price}} 暂无 -->
						</view>
					</view>
					<!-- <view class="price2">
						 可续租 ·可买断 
					</view> -->
				</view>

				<view class="flex-row-space-between">
					<view v-for="(item,index) in labelList" :key="index">
						<view class=" flex-row-flex-start">
							<image class="labelImage" src="/static/lease/label.png" mode="scaleToFill"></image>
							<view class="label">
								{{item}}
							</view>
						</view>
					</view>
				</view>

			</view>



			<view class="infoView1">
				<!-- <view @tap="showSpecFun" class="flex-row-space-between" style="height: 80rpx;">
					<view class="flex-row-flex-start">
						<view class="infoTitle">
							选择
						</view>
						<view class="infoTitle1">
							{{guige}}
						</view>
					</view>
					<u-icon name="arrow-right" size="26" color="#666666"></u-icon>
				</view> -->

				<view class="flex-row-space-between" style="height: 80rpx;">
					<view class="flex-row-flex-start">
						<view class="infoTitle">
							配送方式
						</view>
						<view class="infoTitle1">
							快递
						</view>
					</view>

				</view>

				<view class="flex-row-space-between" style="height: 80rpx;">
					<view class="flex-row-flex-start">
						<view class="infoTitle">
							付款方式
						</view>
						<view class="infoTitle1">
							<text v-if="Number(config.types)==2">全款</text>
							<text v-else>先享后付</text>
						</view>
					</view>

				</view>
			</view>
<!-- 			<view class="shopView flex-row-space-between">
				<view class="flex-row-flex-start">
					<image class="shopImage" :src="shopInfo.shop_logo" mode="aspectFill"></image>
					<view class="">
						<view class="shop_name">
							{{shopInfo.shop_name}}
						</view>
						<view class="describle">
							经营范围:{{shopInfo.op_object}}
						</view>
					</view>

				</view>
				<view @click="shopButtonClick()" class="shopButton flex-column-center">
					进店逛逛
				</view>
			</view> -->

			<!-- 详情信息 || 租赁说明 -->
			<view class="details mt20 bg-white" id="details">
				<u-tabs :list="tabsList" :is-scroll="false" :current="currentTab" :bold="true" font-size="30rpx"
					active-color="#FF4D40" inactive-color="#333333" bar-width="120" bar-height="6"
					@change="changeTab" />
				<!-- 详情内容 -->
				<view class="content">
					<template v-if="!currentTab">
						<!-- <rich-text v-html="goodsDetail.content"> </rich-text> -->
						<u-parse v-if="goodsDetail.content" :html="goodsDetail.content" :lazy-load="true"
							:show-with-animation="true" />
					</template>
					<template v-else-if="currentTab">
						<!-- <rich-text v-html="goodsDetail.lease_explain"> </rich-text> -->
						<u-parse v-if="goodsDetail.lease_explain" :html="goodsDetail.lease_explain" :lazy-load="true"
							:show-with-animation="true" />
					</template>
				</view>
			</view>

			<!-- Widget -->
			
			<!-- #ifndef MP-ALIPAY -->
			<view class="footer row bg-white fixed">
				<view class="btn column-center" @tap="toHome">
					<image class="icon-md" src="/static/lease/home.png" />
					<text class="xxs lighter">首页</text>
				</view>
				<view class="btn column-center" @tap="collectGoodsFun">
					<image class="icon-md"
						:src="goodsDetail.is_collect == 1 ? '/static/images/icon_collection_s.png' : '/static/images/icon_collection.png'" />
					<text class="xxs lighter">收藏</text>
				</view>
				<view class="right-buy br60 white mr20 ml10 md" @tap="showSpecFun">立即下单</view>
			</view>
			<!-- #endif -->
		</view>
		<!-- 找不到商品 -->
		<view v-else>
			<view class="details-null column-center">
				<image class="img-null" src="/static/images/goods_null.png" />
				<view class="xs muted">该商品已下架或不存在，去逛逛别的吧~</view>
			</view>
			<!-- <recommend /> -->
		</view>


		<!-- 商品规格选择Popup -->
		<lease-spec-popup :show="showSpec" :goods="goodsDetail" buttonTxt="去下单" @close="showSpec = false"
			@confirm="onConfirm" />

		<!-- 分享Popup -->
		<!-- 		<share-popup 
          :show="showShareBtn" 
          :goods-id="id"  
		  type="lease_goods"
          :img-url="goodsDetail.image" 
          :summary="goodsDetail.remark" 
          :share-title="goodsDetail.name" 
          @close="showShareBtn = false" 
        /> -->

		<!-- 	<share-popup :show="showShareBtn" @close="showShareBtn = false" :share-id="id"
			pagePath="bundle/pages/lease_goods_details/lease_goods_details" :type="2" :config="{
				avatar: userInfo.avatar,
				nickname: userInfo.nickname,
				image: goodsDetail.image,
				price: price,
				name: goodsDetail.name
		}">
		</share-popup> -->

	</view>
</template>

<script>
	import {
		getLeaseGoodsDetail,
		getGoodsCoupon,
		getGoodsComment,
		leaseBuy,
		leaseCollectGoods,
		orderGetService,
		getShopInfo
	} from '@/api/lease.js'

	import {
		getPoster
	} from '@/api/store';
	import {
		mapGetters
	} from 'vuex';
	import {
		toLogin
	} from '@/utils/login';
	import {
		strToParams,
		getRect
	} from '@/utils/tools'
	const app = getApp()
	export default {
		data() {
			return {
				config: {},
				shopInfo: {},
				guige: '',
				labelList: ['正品保障', '售后无忧', '快速发货', '配件齐全'],
				isFirstLoading: true, // 是否第一次加载
				isNull: false, // 是否找到该商品
				showSpec: false, // 规格选择Popup
				showShareBtn: false, // 分享Popup
				swiperList: [], // 轮播图列表
				goodsDetail: {}, // 商品信息数据
				goodsLike: [], // 猜你喜欢列表数据
				goodsType: 0, // 商品类型
				comment: {}, // 留言数据
				id: '', // 当前商品ID
				currentTab: 0, // Tabs当前位置                
				commentPercent: '', // 好评率
				// Tabs 列表
				tabsList: [{
					kay: 'goods',
					name: '商品详情'
				}, {
					key: 'lease',
					name: '说明'
				}],

				navStyle: {
					backBg: 0.4,
					backgroundBg: 0,
					backColor: 'rgba(256,256,256,1)'
				},
				active: 0,
				topArr: [],
			};
		},

		onPageScroll(e) {
			const top = uni.upx2px(500)
			const {
				scrollTop
			} = e
			const percent = scrollTop / top
			this.navStyle.backgroundBg = percent
			this.navStyle.backBg = 0.4 * (0.5 - percent)
			this.navStyle.backColor = percent < 0.5 ? 'rgba(256,256,256,' + (0.5 - percent) * 2 + ')' : 'rgba(0,0,0,' +
				(
					percent - 0.5) * 2 + ')'
			if (!this.isTouchStart) return
			const topList = this.topArr.map((item, index) => ({
					index,
					top: item
				}))
				.filter(item => item.top <= scrollTop)

			if (topList.length) {
				const index = topList.sort((a, b) => b.top - a.top)[0].index
				if (this.active == index) return
				this.active = index
			}
		},

		computed: {
			...mapGetters(['userInfo']),
			// // 价格
			// price() {
			// 	if (!this.goodsDetail.hasOwnProperty('goods_periods')) return;

			// 	const periods = this.goodsDetail.goods_periods.periods;
			// 	const day = Math.max(...periods.map(item => item.day));
			// 	const period = periods.find(item => item.day == day);
			// 	const basePrice = this.goodsDetail.price;
			// 	const price = basePrice * (period.rent / 100) / period.day * period.periods;

			// 	// return price.toFixed(2);
			// 	return Number(price.toString().match(/^\d+(?:\.\d{0,2})?/));
			// },

			navHeight() {
				return app.globalData.navHeight
			},
		},

		methods: {
			shopButtonClick() {
				var shop_id = uni.getStorageSync('shop_id') ? uni.getStorageSync('shop_id') : 1
				uni.navigateTo({
					url: '/bundle/pages/lease_goods_details/shop?shop_id=' + shop_id
				})
			},
			getShopInfo(id) {
				uni.setStorageSync('shop_id', id)
				getShopInfo({
					shop_id: id
				}).then(({
					code,
					data
				}) => {
					if (code != 1) return;
					this.shopInfo = data
				});
			},
			toHome() {
				console.log('home')
				uni.switchTab({
					url: '/pages/lease/lease'
				})
			},
			changeActive(index) {
				this.isTouchStart = false

				uni.pageScrollTo({
					scrollTop: this.topArr[index],
					duration: 200
				});
			},

			getRectInfo() {
				if (this.topArr.length) return
				getRect('#goods').then((res) => {
					this.topArr[0] = res.top - this.navHeight
				})
				getRect('#evaluation').then((res) => {
					this.topArr[1] = res.top - this.navHeight
				})
				getRect('#details').then((res) => {
					this.topArr[2] = res.top - this.navHeight
				})
			},


			// 改变当前的Tabs位置
			changeTab(index) {
				this.currentTab = index;
			},
			orderGetService() {
				orderGetService({
					id: this.id
				}).then(res => {
					this.$set(this.goodsDetail, 'serviceList', res.data);
				});
			},
			// 获取商品信息
			getGoodsDetailFun() {
				var that = this
				console.log('getLeaseGoodsDetail', this.id)
				getLeaseGoodsDetail({
					id: this.id
				}).then(res => {
					if (res.code == 1) {
						console.log('res.data', res.data)
						const data = res.data;

						that.goodsDetail = data;
						that.swiperList = data.goods_image;
						that.orderGetService()
						that.isFirstLoading = false

						// 	// this.comment = data.comment;
						// 	// this.goodsLike = data.like;

						// 	this.$nextTick(() => {

						// 		this.isFirstLoading = false

						// 		// 滚动到顶部，防止h5端出现问题
						// 		uni.pageScrollTo({
						// 			scrollTop: 0,
						// 			duration: 0,
						// 		});
						// 		this.getRectInfo()
						// 	});
						this.getShopInfo(res.data.shop_id)
					} else {
						this.isNull = true
						this.isFirstLoading = false;
					}
				});
			},

			// 收藏商品
			collectGoodsFun() {
				if (!this.isLogin) return toLogin();

				const is_collect = this.goodsDetail.is_collect;

				leaseCollectGoods({
					status: is_collect == 0 ? 1 : 0,
					id: this.id
				}).then(res => {
					if (res.code != 1) return;

					is_collect == 0 ?
						this.$toast({
							title: '收藏成功'
						}) :
						this.$toast({
							title: '取消收藏'
						});

					this.getGoodsDetailFun();
				});
			},

			// 展示规格
			showSpecFun() {
				this.showSpec = true;
			},

			// 立即租赁
			onConfirm(field) {
				this.showSpec = false;
				// // #ifdef MP-WEIXIN
				// var title=this.config.wechat_log
				// console.log(this.config, "12`11231")
				// this.$toast({
				// 	title: title
				// })
				// return
				// // #endif

				// #ifdef MP-ALIPAY || H5 || APP || MP-WEIXIN
				if (!this.isLogin) {
					this.$toast({
						title: '请先登录'
					})
					setTimeout(() => {
						toLogin();
					}, 2000)

					return
				}
				console.log(field)
				uni.navigateTo({
					url: '/bundle/pages/lease_confirm_order/lease_confirm_order?data=' +
						encodeURIComponent((JSON.stringify(field)))
				})

				// #endif

			}
		},

		onLoad(options) {
			if (options && options.scene) {
				// let scene = strToParams(decodeURIComponent(options.scene));
				options.id = options.scene;
			}

			if (!options.id)
				return this.$toast({
					title: '缺少参数，无法查看商品'
				}, {
					tab: 3
				});

			const goods_id = options.id || 4;
			this.id = goods_id;
			// getGoodsComment({
			// 	lease_goods_id: goods_id
			// }).then(({
			// 	code,
			// 	data
			// }) => {
			// 	if (code != 1) return;
			// 	this.commentPercent = data.percent;
			// });


		},

		onShow() {
			this.config = uni.getStorageSync('config')
			this.getGoodsDetailFun();
		},

		// 分享
		onShareAppMessage() {
			const goodsDetail = this.goodsDetail;
			const userInfo = this.userInfo;

			return {
				title: goodsDetail.name,
				imageUrl: goodsDetail.image,
				path: '/pages/goods_details/goods_details?id=' +
					this.id +
					"&invite_code=" +
					userInfo.distribution_code
			};
		}
	};
</script>

<style lang="scss">
	.goods-details {
		padding-bottom: calc(120rpx + env(safe-area-inset-bottom));

		.lease-tips {
			margin: 0 20rpx;
			padding: 10rpx 18rpx;
			border-radius: 4rpx;
			font-size: 24rpx;
			background-color: #FFFADE;
			color: #F95F2F;
		}

		.goods-info {
			position: relative;
			padding-bottom: 20rpx;

			.info-header {
				padding: 20rpx 0 0rpx 24rpx;

				.price {
					align-items: baseline;
				}
			}

			.goods-tag {
				padding: 8rpx 10rpx;
				margin-right: 20rpx;
				border-radius: 4px;
				font-size: 22rpx;
				background-color: rgba(36, 163, 255, 0.1);
				color: #FF4D40;
			}

			.vip-price {
				margin: 0 24rpx;
				background-color: #FFE9BA;
				line-height: 36rpx;
				border-radius: 6rpx;
				overflow: hidden;

				.price-name {
					background-color: #101010;
					padding: 3rpx 12rpx;
					color: #FFD4B7;
					position: relative;
					overflow: hidden;

					&::after {
						content: '';
						display: block;
						width: 20rpx;
						height: 20rpx;
						position: absolute;
						right: -15rpx;
						background-color: #FFE9BA;
						border-radius: 50%;
						top: 50%;
						transform: translateY(-50%);
						box-sizing: border-box;
					}
				}
			}

			.name {
				padding: 20rpx 24rpx;
				flex: 1;
			}

			.icon-share {
				width: 134rpx;
				height: 60rpx;
			}
		}

		.discount {
			padding: 24rpx;

			.text {
				width: 100rpx;
				flex: none;
			}

			.con {
				width: 400rpx;
			}

			.coupons-item {
				overflow: hidden;

				&>view {
					position: relative;
					height: 40rpx;
					line-height: 40rpx;
					padding: 0 18rpx;
					border-radius: 6rpx;
					box-sizing: border-box;
					background-color: $-color-ornament;
					color: #fff;
					white-space: nowrap;
					overflow: hidden;

					&::after,
					&::before {
						content: '';
						display: block;
						width: 20rpx;
						height: 20rpx;
						position: absolute;
						left: -14rpx;
						background-color: #fff;
						border-radius: 50%;
						border: 1px solid currentColor;
						box-sizing: border-box;
					}

					&::after {
						right: -14rpx;
						left: auto;
					}
				}
			}
		}

		.details-null {
			padding-top: 140rpx;
			margin-bottom: 100rpx;
		}

		.spec {
			// padding: 24rpx 24rpx;
			padding: 10rpx 24rpx;

			.text {
				width: 100rpx;
			}
		}

		.evaluation {
			.title {
				height: 100rpx;
				border-bottom: $-solid-border;
				padding: 0 24rpx;
			}

			.con {
				padding: 30rpx 24rpx;
			}

			.user-info .avatar {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
			}
		}

		.details {
			.title {
				line-height: 88rpx;
				text-align: center;
			}

			&>.content {
				padding: 0 20rpx 20rpx;
				overflow: hidden;

				::v-deep image {
					vertical-align: middle;
				}

				// #ifdef H5
				::v-deep img {
					vertical-align: middle;
				}

				// #endif
				// #ifdef MP-WEIXIN || APP-PLUS
				::v-deep ._img {
					display: block;
				}

				// #endif
			}
		}

		.footer {
			height: 100rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			box-sizing: content-box;
			padding-bottom: env(safe-area-inset-bottom);

			.btn {
				width: 100rpx;
				height: 100rpx;
				position: relative;
				line-height: 1.3;
			}

			.cart-num {
				position: absolute;
				left: 60rpx;
				top: 6rpx;
			}

			.add-cart,
			.right-buy {
				flex: 1;
				text-align: center;
				padding: 16rpx 0;
			}

			.add-cart {
				background-color: rgba(36, 163, 255, 0.1);
			}

			.right-buy {
				background-color: $-color-primary;
			}
		}

		.group-play {
			.title {
				padding: 20rpx 28rpx;
				border-bottom: $-solid-border;
			}

			.steps {
				padding: 20rpx 28rpx;

				.step {
					flex: none;
				}

				.line {
					flex: 1;
					border: 1px dashed #999999;
					margin: 0 20rpx;
				}

				.number {
					border: 1rpx solid #707070;
					width: 28rpx;
					height: 28rpx;
					border-radius: 50%;
					line-height: 28rpx;
					text-align: center;
					margin-right: 6rpx;
				}
			}
		}

		.group-list {
			.group-item {
				padding: 20rpx 24rpx;

				&:not(:last-of-type) {
					border-bottom: $-solid-border;
				}

				.group-btn {
					background: linear-gradient(90deg, #f95f2f 0%, #ff2c3c 100%);
					height: 58rpx;
					padding-left: 28rpx;
					padding-right: 28rpx;
					margin-left: 30rpx;
					box-shadow: 0px 6rpx 12rpx rgba(249, 47, 138, 0.4);
				}
			}
		}
	}

	.priceView {
		padding: 0 20rpx;
		margin: 0 20rpx;
		height: 110rpx;

		background-image: url('../../../static/lease/price.png');
		background-size: 100% 100%;
	}

	.infoView {

		margin: 0 20rpx;
		margin-top: -10rpx;
		padding: 20rpx;
		background: #FFFFFF;
		border-radius: 11rpx 11rpx 11rpx 11rpx;
		opacity: 1;
	}

	.infoView1 {

		margin: 20rpx;

		padding: 20rpx;
		background: #FFFFFF;
		border-radius: 11rpx 11rpx 11rpx 11rpx;
		opacity: 1;
	}

	.price {
		font-size: 20rpx;
		font-family: DIN-Medium, DIN;
		font-weight: 500;
		color: #FFFFFF;
	}

	.price1 {
		font-size: 46rpx;
		font-family: DIN-Medium, DIN;
		font-weight: 500;
		color: #FFFFFF;
	}

	.priceTitle {
		font-size: 25rpx;
		font-family: DIN-Regular, DIN;
		font-weight: 400;
		color: #FFFFFF;
	}

	.priceTitle1 {
		font-size: 34rpx;
		font-family: DIN-Medium, DIN;
		font-weight: 500;
		color: #F1D6BB;
	}

	.name {
		margin-bottom: 15rpx;
		margin-top: 15rpx;
		font-size: 34rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #000000;
	}

	.myImage {
		width: 134rpx;
		height: 36rpx;
	}

	.price2 {
		// margin-left: 10rpx;
		font-size: 23rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
	}

	.labelImage {
		width: 26rpx;
		height: 26rpx;
	}

	.label {
		margin-left: 6rpx;
		font-size: 23rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
	}

	.infoTitle {
		width: 140rpx;
		font-size: 27rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(102, 102, 102, 0.4);
	}

	.infoTitle1 {
		font-size: 27rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
	}

	.shopView {
		margin: 20rpx;
		height: 137rpx;
		background: #FFFFFF;
		border-radius: 11rpx 11rpx 11rpx 11rpx;
		opacity: 1;

	}

	.centerBox {
		// margin: 20rpx;
		background: #FFFFFF;
		margin-top: 20rpx;
		padding: 20rpx;

		.topBox {
			margin-bottom: 20rpx;
			margin-left: 10rpx;
		}

		// border-radius: 12rpx;
		.centerImg {
			width: 690rpx;
			height: 150rpx;
			margin-left: 10rpx;
		}
	}

	.shopImage {
		margin-left: 20rpx;
		margin-right: 10rpx;
		width: 99rpx;
		height: 99rpx;
		border-radius: 11rpx;
		background: #F7F7F7;
		opacity: 1;
	}

	.shopButton {
		margin-right: 26rpx;
		width: 122rpx;
		height: 50rpx;
		background: #FF4D4026;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		font-size: 23rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #FF4D40;
	}

	.shop_name {
		font-size: 27rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #000000;
	}

	.describle {
		margin-top: 10rpx;
		font-size: 23rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
	}
</style>